﻿using MengCC.Plugin.SDK;
using System;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;

namespace MengCC.Plugin.Move
{
    class Program
    {
        static void Main(string[] args)
        {
            PluginContext pluginContext = new PluginContext(args);
            var options = pluginContext.GetOptions<Options>();

            //var options = new Options
            //{
            //    IsFilter = true,
            //    FromDir = "D:\\VSProject\\MengCC\\MengCC.App\\bin\\Release\\netcoreapp3.1\\publish\\WorkSpaces\\2\\yangpu\\publish",
            //    ToDir = "D:\\VSProject\\MengCC\\MengCC.App\\bin\\Release\\netcoreapp3.1\\publish\\WorkSpaces\\2\\yangpu\\publish_new",
            //    FilterType = Options.FilterTypeEnum.MoveInFilters,
            //    Filters = "\\CRM\\\n\\bin\\\n\\web.config"
            //};

            var pattent = string.Join("|", options.Filters.Split('\n').Select(s => $"({s.Replace("\\", "\\\\")})"));
            var regex = new Regex(pattent);


            if (!Directory.Exists(options.FromDir))
            {
                Console.WriteLine($"路径 {options.FromDir} 不存在；");
                return;
            }
            MoveFiles(options.FromDir, options, regex);
            RemoveAll(options.FromDir);
        }

        static void MoveFile(string fromfile, string toFile)
        {
            var toDir = Path.GetDirectoryName(toFile);
            if (!Directory.Exists(toDir)) Directory.CreateDirectory(toDir);
            File.Move(fromfile, toFile,true);
            Console.WriteLine(toFile);
        }

        static void MoveFiles(string dir, Options options, Regex regex)
        {
            foreach (var fromfile in Directory.GetFiles(dir))
            {
                var tofile = fromfile.Replace(options.FromDir, options.ToDir);
                if (options.IsFilter)
                {
                    var filterFile = fromfile.Replace(options.FromDir, "");
                    if (options.FilterType == Options.FilterTypeEnum.MoveInFilters)
                    {
                        if (regex.IsMatch(filterFile))
                            MoveFile(fromfile, tofile);
                    }
                    else if (options.FilterType == Options.FilterTypeEnum.MoveNotInFilters)
                    {
                        if (!regex.IsMatch(filterFile))
                            MoveFile(fromfile, tofile);
                    }
                }
                else MoveFile(fromfile, tofile);
            }
            foreach (var item in Directory.GetDirectories(dir))
            {
                MoveFiles(item, options, regex);
            }
        }
        /// <summary>
        /// 设置文件为普通权限
        /// </summary>
        /// <param name="dir"></param>
        private static void SetFileAttributeAsNomal(string dir)
        {
            foreach (var item in Directory.GetFiles(dir))
            {
                File.SetAttributes(item, FileAttributes.Normal);
            }
            foreach (var item in Directory.GetDirectories(dir))
            {
                SetFileAttributeAsNomal(item);
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="project"></param>
        public static void RemoveAll(string dir)
        {
            try
            {
                DirectoryInfo directoryInfo = new DirectoryInfo(dir);
                directoryInfo.Attributes = FileAttributes.Normal | FileAttributes.Hidden | FileAttributes.System;
                SetFileAttributeAsNomal(dir);
                if (directoryInfo.Exists) directoryInfo.Delete(true);
            }
            catch (Exception ex)
            {
                //log
            }
        }
    }
}
